﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IceTea.Domain.Models;

namespace IceTea.Domain
{
    public class TeamFactory
    {
        public static int CreateProductTeam(Team team, int userId)
        {
            using (IceTeaEntities ite = new IceTeaEntities())
            {
                team.CreatedBy = userId;
                team.CreatedTime = DateTime.Now;
                ite.Teams.AddObject(team);
                if (ite.SaveChanges() <= 0)
                    throw new DbModelException("Failed to create an new team of product");
            }
            return team.Id;
        }

        public static int RequestJoinTeam(int teamId, int userId, IEnumerable<int> roles)
        {
            using (IceTeaEntities ite = new IceTeaEntities())
            {
                var tus = (from tu in ite.TeamUsers 
                           where tu.TeamId == teamId && tu.UserId == userId
                           select tu);
                foreach (int role in roles)
                {
                    if (!tus.Any(tu => tu.RoleId == role))
                    {
                        //If there is no team user record then create new request
                        TeamUser tu = new TeamUser()
                        {
                            TeamId = teamId,
                            UserId = userId,
                            RoleId = role,
                            State = (int)TeamUserState.Requested,
                            CreatedTime = DateTime.Now,
                            UpdatedTime = DateTime.Now,
                        };
                        ite.TeamUsers.AddObject(tu);
                    }
                    else
                    {
                        //if there is already a team user record
                        TeamUser tu = tus.SingleOrDefault(u => u.RoleId == role);
                        if (tu.State != (int)TeamUserState.Approved)
                        {
                            tu.State = (int)TeamUserState.Requested;
                            tu.UpdatedTime = DateTime.Now;
                        }
                    }
                }
                return ite.SaveChanges();

            }
        }

        public static Team GetTeam(int teamId)
        {
            using (IceTeaEntities ite = new IceTeaEntities())
            {
                return ite.Teams.SingleOrDefault(t => t.Id == teamId);
            }
        }

        public static IEnumerable<TeamUser> GetTeamUsers(int teamId)
        {
            using (IceTeaEntities ite = new IceTeaEntities())
            {
                return (from tu in ite.TeamUsers.Include("Users")
                 where tu.TeamId == teamId
                 select tu).ToList();
            }
        }

        public static int UpdateTeamUserState(int teamuserId, int state)
        {
            using (IceTeaEntities ite = new IceTeaEntities())
            {
                var teamuser = ite.TeamUsers.SingleOrDefault(tu => tu.Id == teamuserId);
                if (teamuser != null)
                {
                    teamuser.State = state;
                    teamuser.UpdatedTime = DateTime.Now;
                }
                return ite.SaveChanges();
            }
            return -1;
        }
    }

}
